
var config = require('../../utils/config.js')
var store = require('../../utils/store.js')
var util = require('../../utils/util.js')
var WxParse = require('../../wxParse/wxParse.js');
var API_URL = config.API_URL
var app = getApp();
var goodId;
var num;
var arr;
var getStar = function (num) {
    console.log(num)
    if (num == "") {
        num = 5;
    }
    arr = [];
    for (var i = 0; i < 5; i++) {
        if (i < num) {
            arr.push('star');
        } else {
            arr.push('star-w');
        }
    }
    return arr

}
Page({
    data: {
        hidden: true,
    },
    onLoad: function (options) {
        console.log(options)
        //拿到传过来的商品ID     
        var that = this;
        goodId = options.id;
        var params = {
            goods_id: goodId
        }
        util.PRL(API_URL.good, params)
            .then(res => {
                WxParse.wxParse('description', 'html', res.data.data.goods.description, that, 0);
                that.setData(res.data.data)
            })
        my.httpRequest({
            url: API_URL.good,
            data: params,
            success: (res) => {
                console.log(res.data.data)
                that.setData(res.data.data)
                num = res.data.data.goodsComments[0].starLevel
            },
        });
        // setTimeout(function () {

        //     getStar(num)
        // }, 1000);
        //    getStar(res.data.data.goodsComments[0].starLevel)
    },
    onShow: function () {
        var that = this;
        setTimeout(function () {
            console.log(num)
            getStar(num)
            that.setData({
                arr: arr
            })
            console.log(that.data.arr)
              that.getImg()
        }, 1000);
       
    },
    getImg: function (data) {
        console.log(this.data.goodsComments)
        var a = data
        console.log(a)
    },
    // 服务图标切换
    show: function () {
        this.setData({
            hidden: false
        })
    },
    hidden: function () {
        this.setData({
            hidden: true
        })
    },
    // 收藏
    collection: function () {
        var that = this;
        var goods = that.data.goods;
        var collection = goods.hasCollected;
        var url;
        if (collection == 0) {
            url = API_URL.collect_add;
            my.showToast({
                content: '收藏成功',
                duration: 1000
            });
        } else {
            url = API_URL.collect_del;
            my.showToast({
                content: '取消收藏',
                duration: 1000
            });
        }
        let params = {
            item_id: goodId,
            type: 'goods',
            goods_id: goodId
        }
        util.PRL(url, params)
            .then(res => {
                console.log(res);
                if (res.data.status == 200) {
                    goods.hasCollected = collection == 0 ? 1 : 0;
                    that.setData({
                        goods: goods
                    })
                }
            }).catch(res => {

            })
    },
    // 购物车跳转
    cart: function (e) {
        my.redirectTo({
            url: '/pages/cart/cart',
        })
    },
    // 加入购物车
    add_cart: function (e) {
        var that = this;
        let params = {
            goods_id: goodId
        }
        util.PRL(API_URL.add_cart, params)
            .then(res => {
                if (res.data.status == 200) {
                    my.showToast({
                        content: '添加成功',
                        type: 'success',
                        duration: 800
                    })
                } else {
                    my.showToast({
                        content: res.data.msg,
                        image: '/images/error.png',
                        duration: 800
                    })
                }
            }).catch(res => {
                console.log(res)
            })
    },
    // 购买
    buy: function (e) {
        var that = this;
        var good = {
            id: goodId,
            img: that.data.goods.image,
            name: that.data.goods.name,
            price: that.data.goods.price
        }
        app.globalData.good = good;
        console.log(good)

        my.redirectTo({
            url: '/pages/demand/order/order'
        })
    },
    // 跳转更多评论页面
    comment: function () {
        my.navigateTo({
            url: '/pages/good/comment/comment?id=' + goodId,
        })
    },
    // 跳转评论页面
    commentInfo: function (e) {
        let id = e.currentTarget.dataset.id;
        my.navigateTo({
            url: '/pages/good/comment_detail/comment_detail?id=' + id,
        })
    }
})